/**
 * Copyright 2007 Igor Chaves Cananéa
 *
 * This file is part of PBSFW
 *
 * PBSFW is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * PBSFW is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "sequencereporter.h"

namespace pbsfw {
    void SequenceReporter::reset() {
        map<Property*, uint>::iterator itr = this->currentSamples.begin();
        map<Property*, uint>::iterator end = this->currentSamples.end();

        while (itr != end) {
            Property *p = itr->first;
            if (this->currentSamples[p] > this->maxSamples[p])
                this->maxSamples[p] = this->currentSamples[p];
            if (this->stopSize[p] > this->currentSamples[p])
                this->stopSize[p] = this->currentSamples[p];
            this->currentSamples[p] = 0;
            ++itr;
        }
    }

    void SequenceReporter::simulationEnd() {
        map<Property*, uint>::iterator itr = this->stopSize.begin();
        map<Property*, uint>::iterator end = this->stopSize.end();
        const char *printString = "SE\t%s\t%s\t%s\t%.3lf\t%.3lf\t%.3lf\t"
                                    "%.3lf\t%.3lf\t%.3lf\t%.3lf\t%d\t%.3lf\t"
                                    "%.3lf\t%d\n";

        while (itr != end) {
            Property *p = itr->first;
            uint stop = itr->second;
            vector<Statistic*> &v = this->data[p];

            for (uint i = 0; i < stop; ++i) {
                Statistic *stat = v[i];

                fprintf(this->output, printString, p->getName(), p->getOwner(),
                        p->getSecondName(), this->times[p][i],
                        stat->getSampleMin(), stat->getSampleMax(),
                        stat->getSampleMean(), stat->getSampleStandardDeviation(),
                        stat->getConfidenceInterval(),stat->getPrecision(), stat->getSampleSize(),
                        stat->getConfidenceLevel(), stat->getAccuracy(),
                        stat->getRequiredNumberOfReplications());
            }
            ++itr;
        }
    }
};
